{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Libsvm GUI\n\n\nA simple graphical frontend for Libsvm mainly intended for didactic\npurposes. You can create data points by point and click and visualize\nthe decision region induced by different kernels and parameter settings.\n\nTo create positive examples click the left mouse button; to create\nnegative examples click the right button.\n\nIf all examples are from the same class, it uses a one-class SVM.\n\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(__doc__)\n\n# Author: Peter Prettenhoer <peter.prettenhofer@gmail.com>\n#\n# License: BSD 3 clause\n\nimport matplotlib\nmatplotlib.use('TkAgg')\n\nfrom matplotlib.backends.backend_tkagg import FigureCanvasTkAgg\nfrom matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg\nfrom matplotlib.figure import Figure\nfrom matplotlib.contour import ContourSet\n\nimport sys\nimport numpy as np\nimport tkinter as Tk\n\nfrom sklearn import svm\nfrom sklearn.datasets import dump_svmlight_file\n\ny_min, y_max = -50, 50\nx_min, x_max = -50, 50\n\n\nclass Model:\n    \"\"\"The Model which hold the data. It implements the\n    observable in the observer pattern and notifies the\n    registered observers on change event.\n    \"\"\"\n\n    def __init__(self):\n        self.observers = []\n        self.surface = None\n        self.data = []\n        self.cls = None\n        self.surface_type = 0\n\n    def changed(self, event):\n        \"\"\"Notify the observers. \"\"\"\n        for observer in self.observers:\n            observer.update(event, self)\n\n    def add_observer(self, observer):\n        \"\"\"Register an observer. \"\"\"\n        self.observers.append(observer)\n\n    def set_surface(self, surface):\n        self.surface = surface\n\n    def dump_svmlight_file(self, file):\n        data = np.array(self.data)\n        X = data[:, 0:2]\n        y = data[:, 2]\n        dump_svmlight_file(X, y, file)\n\n\nclass Controller:\n    def __init__(self, model):\n        self.model = model\n        self.kernel = Tk.IntVar()\n        self.surface_type = Tk.IntVar()\n        # Whether or not a model has been fitted\n        self.fitted = False\n\n    def fit(self):\n        print(\"fit the model\")\n        train = np.array(self.model.data)\n        X = train[:, 0:2]\n        y = train[:, 2]\n\n        C = float(self.complexity.get())\n        gamma = float(self.gamma.get())\n        coef0 = float(self.coef0.get())\n        degree = int(self.degree.get())\n        kernel_map = {0: \"linear\", 1: \"rbf\", 2: \"poly\"}\n        if len(np.unique(y)) == 1:\n            clf = svm.OneClassSVM(kernel=kernel_map[self.kernel.get()],\n                                  gamma=gamma, coef0=coef0, degree=degree)\n            clf.fit(X)\n        else:\n            clf = svm.SVC(kernel=kernel_map[self.kernel.get()], C=C,\n                          gamma=gamma, coef0=coef0, degree=degree)\n            clf.fit(X, y)\n        if hasattr(clf, 'score'):\n            print(\"Accuracy:\", clf.score(X, y) * 100)\n        X1, X2, Z = self.decision_surface(clf)\n        self.model.clf = clf\n        self.model.set_surface((X1, X2, Z))\n        self.model.surface_type = self.surface_type.get()\n        self.fitted = True\n        self.model.changed(\"surface\")\n\n    def decision_surface(self, cls):\n        delta = 1\n        x = np.arange(x_min, x_max + delta, delta)\n        y = np.arange(y_min, y_max + delta, delta)\n        X1, X2 = np.meshgrid(x, y)\n        Z = cls.decision_function(np.c_[X1.ravel(), X2.ravel()])\n        Z = Z.reshape(X1.shape)\n        return X1, X2, Z\n\n    def clear_data(self):\n        self.model.data = []\n        self.fitted = False\n        self.model.changed(\"clear\")\n\n    def add_example(self, x, y, label):\n        self.model.data.append((x, y, label))\n        self.model.changed(\"example_added\")\n\n        # update decision surface if already fitted.\n        self.refit()\n\n    def refit(self):\n        \"\"\"Refit the model if already fitted. \"\"\"\n        if self.fitted:\n            self.fit()\n\n\nclass View:\n    \"\"\"Test docstring. \"\"\"\n    def __init__(self, root, controller):\n        f = Figure()\n        ax = f.add_subplot(111)\n        ax.set_xticks([])\n        ax.set_yticks([])\n        ax.set_xlim((x_min, x_max))\n        ax.set_ylim((y_min, y_max))\n        canvas = FigureCanvasTkAgg(f, master=root)\n        canvas.show()\n        canvas.get_tk_widget().pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)\n        canvas._tkcanvas.pack(side=Tk.TOP, fill=Tk.BOTH, expand=1)\n        canvas.mpl_connect('button_press_event', self.onclick)\n        toolbar = NavigationToolbar2TkAgg(canvas, root)\n        toolbar.update()\n        self.controllbar = ControllBar(root, controller)\n        self.f = f\n        self.ax = ax\n        self.canvas = canvas\n        self.controller = controller\n        self.contours = []\n        self.c_labels = None\n        self.plot_kernels()\n\n    def plot_kernels(self):\n        self.ax.text(-50, -60, \"Linear: $u^T v$\")\n        self.ax.text(-20, -60, r\"RBF: $\\exp (-\\gamma \\| u-v \\|^2)$\")\n        self.ax.text(10, -60, r\"Poly: $(\\gamma \\, u^T v + r)^d$\")\n\n    def onclick(self, event):\n        if event.xdata and event.ydata:\n            if event.button == 1:\n                self.controller.add_example(event.xdata, event.ydata, 1)\n            elif event.button == 3:\n                self.controller.add_example(event.xdata, event.ydata, -1)\n\n    def update_example(self, model, idx):\n        x, y, l = model.data[idx]\n        if l == 1:\n            color = 'w'\n        elif l == -1:\n            color = 'k'\n        self.ax.plot([x], [y], \"%so\" % color, scalex=0.0, scaley=0.0)\n\n    def update(self, event, model):\n        if event == \"examples_loaded\":\n            for i in range(len(model.data)):\n                self.update_example(model, i)\n\n        if event == \"example_added\":\n            self.update_example(model, -1)\n\n        if event == \"clear\":\n            self.ax.clear()\n            self.ax.set_xticks([])\n            self.ax.set_yticks([])\n            self.contours = []\n            self.c_labels = None\n            self.plot_kernels()\n\n        if event == \"surface\":\n            self.remove_surface()\n            self.plot_support_vectors(model.clf.support_vectors_)\n            self.plot_decision_surface(model.surface, model.surface_type)\n\n        self.canvas.draw()\n\n    def remove_surface(self):\n        \"\"\"Remove old decision surface.\"\"\"\n        if len(self.contours) > 0:\n            for contour in self.contours:\n                if isinstance(contour, ContourSet):\n                    for lineset in contour.collections:\n                        lineset.remove()\n                else:\n                    contour.remove()\n            self.contours = []\n\n    def plot_support_vectors(self, support_vectors):\n        \"\"\"Plot the support vectors by placing circles over the\n        corresponding data points and adds the circle collection\n        to the contours list.\"\"\"\n        cs = self.ax.scatter(support_vectors[:, 0], support_vectors[:, 1],\n                             s=80, edgecolors=\"k\", facecolors=\"none\")\n        self.contours.append(cs)\n\n    def plot_decision_surface(self, surface, type):\n        X1, X2, Z = surface\n        if type == 0:\n            levels = [-1.0, 0.0, 1.0]\n            linestyles = ['dashed', 'solid', 'dashed']\n            colors = 'k'\n            self.contours.append(self.ax.contour(X1, X2, Z, levels,\n                                                 colors=colors,\n                                                 linestyles=linestyles))\n        elif type == 1:\n            self.contours.append(self.ax.contourf(X1, X2, Z, 10,\n                                                  cmap=matplotlib.cm.bone,\n                                                  origin='lower', alpha=0.85))\n            self.contours.append(self.ax.contour(X1, X2, Z, [0.0], colors='k',\n                                                 linestyles=['solid']))\n        else:\n            raise ValueError(\"surface type unknown\")\n\n\nclass ControllBar:\n    def __init__(self, root, controller):\n        fm = Tk.Frame(root)\n        kernel_group = Tk.Frame(fm)\n        Tk.Radiobutton(kernel_group, text=\"Linear\", variable=controller.kernel,\n                       value=0, command=controller.refit).pack(anchor=Tk.W)\n        Tk.Radiobutton(kernel_group, text=\"RBF\", variable=controller.kernel,\n                       value=1, command=controller.refit).pack(anchor=Tk.W)\n        Tk.Radiobutton(kernel_group, text=\"Poly\", variable=controller.kernel,\n                       value=2, command=controller.refit).pack(anchor=Tk.W)\n        kernel_group.pack(side=Tk.LEFT)\n\n        valbox = Tk.Frame(fm)\n        controller.complexity = Tk.StringVar()\n        controller.complexity.set(\"1.0\")\n        c = Tk.Frame(valbox)\n        Tk.Label(c, text=\"C:\", anchor=\"e\", width=7).pack(side=Tk.LEFT)\n        Tk.Entry(c, width=6, textvariable=controller.complexity).pack(\n            side=Tk.LEFT)\n        c.pack()\n\n        controller.gamma = Tk.StringVar()\n        controller.gamma.set(\"0.01\")\n        g = Tk.Frame(valbox)\n        Tk.Label(g, text=\"gamma:\", anchor=\"e\", width=7).pack(side=Tk.LEFT)\n        Tk.Entry(g, width=6, textvariable=controller.gamma).pack(side=Tk.LEFT)\n        g.pack()\n\n        controller.degree = Tk.StringVar()\n        controller.degree.set(\"3\")\n        d = Tk.Frame(valbox)\n        Tk.Label(d, text=\"degree:\", anchor=\"e\", width=7).pack(side=Tk.LEFT)\n        Tk.Entry(d, width=6, textvariable=controller.degree).pack(side=Tk.LEFT)\n        d.pack()\n\n        controller.coef0 = Tk.StringVar()\n        controller.coef0.set(\"0\")\n        r = Tk.Frame(valbox)\n        Tk.Label(r, text=\"coef0:\", anchor=\"e\", width=7).pack(side=Tk.LEFT)\n        Tk.Entry(r, width=6, textvariable=controller.coef0).pack(side=Tk.LEFT)\n        r.pack()\n        valbox.pack(side=Tk.LEFT)\n\n        cmap_group = Tk.Frame(fm)\n        Tk.Radiobutton(cmap_group, text=\"Hyperplanes\",\n                       variable=controller.surface_type, value=0,\n                       command=controller.refit).pack(anchor=Tk.W)\n        Tk.Radiobutton(cmap_group, text=\"Surface\",\n                       variable=controller.surface_type, value=1,\n                       command=controller.refit).pack(anchor=Tk.W)\n\n        cmap_group.pack(side=Tk.LEFT)\n\n        train_button = Tk.Button(fm, text='Fit', width=5,\n                                 command=controller.fit)\n        train_button.pack()\n        fm.pack(side=Tk.LEFT)\n        Tk.Button(fm, text='Clear', width=5,\n                  command=controller.clear_data).pack(side=Tk.LEFT)\n\n\ndef get_parser():\n    from optparse import OptionParser\n    op = OptionParser()\n    op.add_option(\"--output\",\n                  action=\"store\", type=\"str\", dest=\"output\",\n                  help=\"Path where to dump data.\")\n    return op\n\n\ndef main(argv):\n    op = get_parser()\n    opts, args = op.parse_args(argv[1:])\n    root = Tk.Tk()\n    model = Model()\n    controller = Controller(model)\n    root.wm_title(\"Scikit-learn Libsvm GUI\")\n    view = View(root, controller)\n    model.add_observer(view)\n    Tk.mainloop()\n\n    if opts.output:\n        model.dump_svmlight_file(opts.output)\n\nif __name__ == \"__main__\":\n    main(sys.argv)"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.6.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}